package bb.lanxing.device.home

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import bb.lanxing.R
import bb.lanxing.databinding.ActivityXossDeviceHomeBinding
import bb.lanxing.device.sgdevcie.home.XossDeviceHomeSGFragment
import bb.lanxing.guide.GuideHelper
import bb.lanxing.guide.device.GuideBuilder
import bb.lanxing.manager.SharedManager
import bb.lanxing.mvvm.base.BaseDBActivity
import bb.lanxing.util.text.TextUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class XossDeviceHomeActivity : BaseDBActivity<ActivityXossDeviceHomeBinding>() {
    var isGuideShown: Boolean = false

    override fun registerMessageEvent(): Boolean {
        return true
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_xoss_device_home
    }

    override fun initView(binding: ActivityXossDeviceHomeBinding) {
        var stringExtra = intent.getStringExtra("EXTRA_BIKE_COMPUTER_GENERATION") ?: ""
        var fragment: Fragment? = null
        if (stringExtra == "IS_FG") {
//            fragment =  XossDeviceHomeFGFragment();
        } else if (stringExtra == "IS_SG") {
            fragment = XossDeviceHomeSGFragment()
        }
        if (fragment != null) {
            val extras = intent.extras
            if (extras != null) {
                fragment.arguments = extras
            }
            val supportFragmentManager = supportFragmentManager
            Log.d("wangsm_bt"," XossDeviceHomeActivity,initView,stringExtra: $stringExtra")
            // Log.d("wangsm_bt"," wangsm,device address: $pairTarget.address")
            if (fragment.isAdded || supportFragmentManager.findFragmentByTag(stringExtra) != null) {
                return
            }
            getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, fragment, stringExtra).commit()
        }
    }

    private fun showGuideIfFirstTimeConnect(str: String) {
        lifecycleScope.launch(Dispatchers.Main) {
            if (!isGuideShown) {
                isGuideShown = true
                if (!TextUtils.isEmptyOrNull(str)) {
                    if (SharedManager.getInstance().isThisDeviceFirstConnect(str)) {
                        GuideHelper.showFGDeviceHomeGuide(this@XossDeviceHomeActivity,
                            this@XossDeviceHomeActivity.findViewById(R.id.ll_storage),
                            object : GuideBuilder.OnVisibilityChangedListener {
                                override fun onDismiss() {
                                    SharedManager.getInstance()
                                        .setThisDeviceFirstConnect(str, false)
                                    sendMessage(R.id.msg_go_to_storage_fg)
                                }

                                override fun onShown() {
                                }
                            })
                    }
                }
            }
        }
    }

    override fun onMessage(i: Int, i2: Int, i3: Int, obj: Any, bundle: Bundle): Boolean {
        when (i) {
            R.id.msg_close_device_home -> {
                finish()
                return true
            }

            R.id.msg_show_guide_fg -> {
                val extras = intent.extras
                showGuideIfFirstTimeConnect(extras?.getString("EXTRA_DEVICE_ADDRESS") ?: "")
                return true
            }

            else -> {
                return false
            }
        }
    }


    companion object {
        fun launch(context: Context, deviceAddress: String, deviceName: String, i: Int) {
            val intent = Intent(context, XossDeviceHomeActivity::class.java)
            intent.putExtra("EXTRA_DEVICE_ADDRESS", deviceAddress)
            intent.putExtra("EXTRA_DEVICE_TYPE", i)
            intent.putExtra("EXTRA_DEVICE_NAME", deviceName)
           // Log.d("wangsm_bt"," XossDeviceHomeActivity,initView,stringExtra: $stringExtra")
            Log.d("wangsm_bt"," XossDeviceHomeActivity,deviceAddress: $deviceAddress")
            Log.d("wangsm_bt"," XossDeviceHomeActivity,EXTRA_DEVICE_TYPE: $i")
            Log.d("wangsm_bt"," XossDeviceHomeActivity,deviceName: $deviceName")
            Log.d("wangsm_bt"," XossDeviceHomeActivity,launch start activity")
            context.startActivity(intent)
        }
    }
}
